package com.yc.cloud.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.admin.dto.request.SysApplicationVersionCreateParam;
import com.yc.cloud.admin.dto.request.SysApplicationVersionListParam;
import com.yc.cloud.admin.dto.request.SysApplicationVersionUpdateParam;
import com.yc.cloud.admin.service.SysApplicationService;
import com.yc.cloud.admin.service.SysApplicationVersionService;
import com.yc.cloud.api.vo.SysApplicationVersionVo;
import com.yc.cloud.basic.constants.TipsConstants;
import com.yc.cloud.basic.dto.BaseQueryParam;
import com.yc.cloud.basic.exception.Asserts;
import com.yc.cloud.entity.SysApplicationVersion;
import com.yc.cloud.mapper.SysApplicationVersionMapper;
import com.yc.cloud.oss.service.OssFileService;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2024-04-20
 */
@Service
public class SysApplicationVersionServiceImpl extends ServiceImpl<SysApplicationVersionMapper, SysApplicationVersion> implements SysApplicationVersionService {


    private final OssFileService ossFileService;
    private final SysApplicationService sysApplicationService;

    public SysApplicationVersionServiceImpl(OssFileService ossFileService, SysApplicationService sysApplicationService) {
        this.ossFileService = ossFileService;
        this.sysApplicationService = sysApplicationService;
    }

    @Override
    public IPage<SysApplicationVersionVo> list(SysApplicationVersionListParam param, BaseQueryParam queryParam) {
        val query = new QueryWrapper<SysApplicationVersion>();
        IPage<SysApplicationVersion> page;
        if (param.getApplicationId() != null) {
            query.eq(SysApplicationVersion.APPLICATION_ID, param.getApplicationId());
        }
        //排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(queryParam.getSortBy());
            } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(queryParam.getSortBy());
            }
        }
        //模糊查询
//        if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
//            query.and(queryWrapper -> {
//            });
//        }
        //分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), baseMapper.selectCount(query), true);
        }
        val result = baseMapper.selectPage(page, query);
        return result.convert(this::convertVo);
    }

    @Override
    public SysApplicationVersion create(SysApplicationVersionCreateParam param) {
        if (sysApplicationService.getById(param.getApplicationId()) == null) {
            Asserts.fail(TipsConstants.APPLICATION_NOT_EXIST);
        }
        var sysapplicationversion = getByVersionNumberAndApplicationId(param.getVersionNumber(), param.getApplicationId());
        if (sysapplicationversion != null) {
            Asserts.fail(TipsConstants.APPLICATION_VERSION_EXIST);
        }
        sysapplicationversion = new SysApplicationVersion();
        BeanUtils.copyProperties(param, sysapplicationversion);
        val saveFlag = save(sysapplicationversion);
        return saveFlag ? sysapplicationversion : null;
    }

    private SysApplicationVersion getByVersionNumberAndApplicationId(Integer versionNumber, Long applicationId) {
        return baseMapper.selectOne(new QueryWrapper<SysApplicationVersion>()
                .eq(SysApplicationVersion.VERSION_NUMBER, versionNumber)
                .eq(SysApplicationVersion.APPLICATION_ID, applicationId));
    }

    @Override
    public SysApplicationVersion get(Long id) {
        return getById(id);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public SysApplicationVersion update(Long id, SysApplicationVersionUpdateParam param) {
        val sysapplicationversion = new SysApplicationVersion();
        BeanUtils.copyProperties(param, sysapplicationversion);
        sysapplicationversion.setId(id);
        val updateFlag = updateById(sysapplicationversion);
        return updateFlag ? sysapplicationversion : null;
    }


    @Override
    public SysApplicationVersionVo convertVo(SysApplicationVersion sysapplicationversion) {
        if (sysapplicationversion == null) {
            return null;
        }
        val vo = new SysApplicationVersionVo();
        BeanUtils.copyProperties(sysapplicationversion, vo);
        vo.setUrl(ossFileService.getOssFileUrl(String.valueOf(sysapplicationversion.getFileId())));
        val application = sysApplicationService.getById(sysapplicationversion.getApplicationId());
        vo.setApplication(application);
        return vo;
    }

    @Override
    public List<SysApplicationVersionVo> convertVo(List<SysApplicationVersion> SysApplicationVersionList) {
        if (CollUtil.isNotEmpty(SysApplicationVersionList)) {
            return SysApplicationVersionList.stream().map(this::convertVo).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
